﻿Imports LecturerAppointmentSystem.UserDetails
Imports System.Data.OleDb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.Security

Public Class ConfirmationSent
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If String.IsNullOrEmpty(Request.QueryString("UserId")) Then
            lbl_VerificationMessageLabel.Text = "No user id was specified....."
        Else
            Dim userId As Integer
            Try
                userId = Request.QueryString("UserId")
            Catch ex As Exception
                lbl_VerificationMessageLabel.Text = "The user id specified in not in the proper format"
                Return
            End Try
            Dim allUsers As Array = getAllUsersById()
            If doesUserExist(allUsers, userId) = False Then
                lbl_VerificationMessageLabel.Text = "No corresponding user account could be found..."
            Else
                Dim sql As String = "SELECT * FROM aspnet_Membership WHERE UserId = @UserId"
                Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
                cmd.Parameters.AddWithValue("@UserId", userId)
                conn.Open()
                Dim reader As OleDbDataReader = cmd.ExecuteReader()
                Dim approved As String = ""
                If reader.Read() Then
                    approved = reader("IsApproved").ToString()
                End If
                conn.Close()
                If approved = True Then
                    lbl_VerificationMessageLabel.Text = "The specified user account was previously activated..."
                    Return
                ElseIf approved = False Then
                    Dim approveUserSql As String = "UPDATE aspnet_Membership SET IsApproved = @IsApproved WHERE UserId = UserId"
                    Dim approveUserCmd As OleDbCommand = New OleDbCommand(approveUserSql, conn)
                    approveUserCmd.Parameters.AddWithValue("@IsApproved", True)
                    approveUserCmd.Parameters.AddWithValue("UserId", userId)
                    conn.Open()
                    approveUserCmd.ExecuteNonQuery()
                    conn.Close()
                    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority) & Request.ApplicationPath
                    activate.Visible = True
                    lbl_VerificationMessageLabel.Text = "Your account has been successfully activated."
                Else
                    lbl_VerificationMessageLabel.Text = "An unknown error has occured. Please follow the activation link and try again. If you still get this error please contact us quoting this error"
                End If
            End If
        End If
    End Sub

End Class